import 'dart:math';

import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('Hi,Flutter'),
      // ),
      //SafeArea
      body: CustomScrollViewDemo2(),
    );
  }
}

class CustomScrollViewDemo2 extends StatelessWidget {
  const CustomScrollViewDemo2({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          pinned: true,
          // title: Text('Hi,Flutter'),
          expandedHeight: 300,
          flexibleSpace: FlexibleSpaceBar(
            title: Text('Hi,Flutter'),
            background: Image.asset('assets/images/tupian.jpeg',fit: BoxFit.cover,),
          ),
        ),
        SliverGrid(
            delegate: SliverChildBuilderDelegate(
                  (BuildContext ctx, int index) => Container(
                color: Color.fromARGB(255, Random().nextInt(256),
                    Random().nextInt(256), Random().nextInt(256)),
              ),
              childCount: 10,
            ),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 8,
              mainAxisSpacing: 2,
              childAspectRatio: 1.5,
            )),
        SliverList(
            delegate: SliverChildBuilderDelegate(
                    (BuildContext ctx, int index) => ListTile(
                  leading: Icon(Icons.people),
                  title: Text('联系人$index'),
                ))),
      ],
    );
  }
}

class CustomScrollViewDemo1 extends StatelessWidget {
  const CustomScrollViewDemo1({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverSafeArea(
          sliver: SliverPadding(
            padding: EdgeInsets.all(8),
            sliver: SliverGrid(
                delegate: SliverChildBuilderDelegate(
                      (BuildContext ctx, int index) => Container(
                    color: Color.fromARGB(255, Random().nextInt(256),
                        Random().nextInt(256), Random().nextInt(256)),
                  ),
                  childCount: 100,
                ),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  crossAxisSpacing: 8,
                  mainAxisSpacing: 2,
                  childAspectRatio: 1.5,
                )),
          ),
        ),
      ],
    );
  }
}
